package com.tianqu.tms.www.member.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;

/**
 * 根据会员ID进入会员信息查看详细页面Controller
 * 
 * @author tianshifu
 * 
 */
public class MemberUDBaseDetailController extends TmsController {

	private static final String MEMBERID = "memId";// 会员ID

	private Logger logger = Logger
			.getLogger(MemberUDBaseDetailController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();

			String memberId = (String) uc.getAttribute(MEMBERID);

			Member entity = Member.getInstance();

			// 通过memberID从member表中获取会员信息,set到viewhelp中
			entity = entity.findMemberInfoByMemId(memberId);
			MemberInputViewHelper view = new MemberInputViewHelper();

			view = setViewFromEntity(view, entity);
		
			uc.removeAttribute(TmsConstants.VIEWHELP);
			uc.setAttribute(TmsConstants.VIEWHELP, view);// 将这个iew直接打到前台
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 
	 * 将entity信息set到viewhelp中
	 */
	private MemberInputViewHelper setViewFromEntity(MemberInputViewHelper view,
			Member entity) {
		//商户编号
		view.setMemberId(String.valueOf(entity.getMemId()));
		//商户名称
		view.setRealName(entity.getRealName());
		//商户短名
		view.setShortName(entity.getShortName());
		//商户名称拼音
		view.setRealNamePy(entity.getRealNamePy());
		//商户状态
		if(entity.getMemStatus() == 1){
			view.setMemStatus("正常");
		}else if(entity.getMemStatus() == 3){
			view.setMemStatus("禁用");
		}
		//注册时间
		view.setInsertTime(entity.getInsertTime());
		//联系人
		view.setContactOwner(entity.getContactOwner());
		//联系人电话
		view.setContactPhone(entity.getContactPhone());
		//法人名称
		view.setLegal(entity.getLegal());
		//法人电话
		view.setPhone(entity.getPhone());
		//法人身份证号
		view.setCardNo(entity.getCardNo());
		//法人地址
		view.setOrgNo(entity.getOrgNo());
		//法人性别
		if(entity.getRegisteredCapital() == 0){
			view.setRegisteredCapital("");
		}else if(entity.getRegisteredCapital() == 1){
			view.setRegisteredCapital("男");
		}else if(entity.getRegisteredCapital() == 2){
			view.setRegisteredCapital("女");
		}
		//法人生日
		String str = entity.getSettlePhone();
		String birthday = "";
		if(!StringUtil.isNullOrBlank(str)){
			birthday = str.substring(0, 4) + "-" + str.substring(4, 6) + "-" + str.substring(6, 8);					
		}
		view.setSettlePhone(birthday);
		view.setMemberZipCode(entity.getMemberZipCode());//商户门头照
		view.setOrgPhoto(entity.getOrgPhoto());// 组织机构代码证图片
		view.setBusinessPhoto(entity.getBusinessPhoto());// 营业执照编号图片
		view.setCardPhoto(entity.getCardPhoto());// 法人身份证图片
		//备注
		view.setCarPortInstr(entity.getCarPortInstr());
		//用户名
		view.setUserId(entity.getUserId());
		//密码
		view.setPassword(entity.getPassword());
		
		return view;
	}
}
